﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace MetaSharp.Transformation.Tests.Patterns
{
	public class ZeroOrOneTests
	{
        [Fact]
		public async Task NoMatchesSucceeds()
		{
            var p = Pattern.Optional(Pattern.Any());
            var m = await Pattern.Match(p, "");
            Assert.True(m.Matched);
		}

        [Fact]
		public async Task SingleMatchSucceeds()
		{
            var p = Pattern.Optional(Pattern.Any());
            var m = await Pattern.Match(p, "a");
			Assert.Equal('a', m.Value);
		}

        [Fact]
		public async Task OnlyMatchesASingleElement()
		{
            var p = Pattern.Optional(Pattern.Any());
            var m = await Pattern.Match(p, "aa");
            Assert.Equal(1, m.End.Stream.Index);
		}

        [Fact]
        public async Task MatchesAndReadsToCorrectPosition()
        {
            var p = Pattern.And(
                Pattern.Optional(Pattern.Value('a')),
                Pattern.Optional(Pattern.Value('b')),
                Pattern.Optional(Pattern.Value('c')));
            var m = await Pattern.Match(p, "ac");
            Assert.Equal(new object[] { 'a', null, 'c' }, m.Value);
		}
	}
}
